const { STRING, DATE, VIRTUAL, INTEGER } = require("sequelize");
const config = require("./config.json");

/**
 * 通用字段
 */
const COMMON_FIELDS = {
  createdAt: {
    field: "CREATED_AT",
    type: DATE,
    comment: "创建时间"
  },
  updatedAt: {
    field: "UPDATED_AT",
    type: DATE,
    comment: "修改时间"
  }
};

/**
 * 通用虚拟字段
 */
const COMMON_VIRTUAL_FIELDS = {
  createTime: {
    type: VIRTUAL,
    get() {
      return new Date(this.getDataValue("createdAt")).valueOf();
    }
  },
  updateTime: {
    type: VIRTUAL,
    get() {
      return new Date(this.getDataValue("updatedAt")).valueOf();
    }
  }
};

module.exports = {
  model: {
    platform: {
      table: "ST_PLT",
      columns: {
        pltId: {
          field: "PLT_ID",
          type: STRING,
          primaryKey: true,
          comment: "流媒体平台ID"
        },
        pltName: {
          field: "PLT_NAME",
          type: STRING(200),
          comment: "流媒体平台名称"
        },
        pltProtocol: {
          field: "PLT_PROTOCOL",
          type: STRING(20),
          comment: "流媒体平台访问协议"
        },
        pltIp: {
          field: "PLT_IP",
          type: STRING(200),
          comment: "流媒体平台域名"
        },
        pltPort: {
          field: "PLT_PORT",
          type: STRING(10),
          comment: "流媒体平台端口"
        },
        pltSecret: {
          field: "PLT_SECRET",
          type: STRING(200),
          comment: "流媒体平台秘钥"
        },
        pltState: {
          field: "PLT_STATE",
          type: STRING(1),
          comment: "流媒体平台状态",
          defaultValue: "1"
        },
        pltRemark: {
          field: "PLT_REMARK",
          type: STRING(500),
          comment: "备注"
        },
        ...COMMON_FIELDS
      },
      virtuals: {
        pltStateName: {
          type: VIRTUAL,
          get() {
            return this.getDataValue("pltState") === "1" ? "启用" : "停用";
          }
        },
        ...COMMON_VIRTUAL_FIELDS
      }
    },
    application: {
      table: "ST_APP",
      columns: {
        appId: {
          field: "APP_ID",
          type: STRING,
          primaryKey: true,
          comment: "应用ID"
        },
        pltId: {
          field: "PLT_ID",
          type: STRING,
          comment: "流媒体服务平台ID"
        },
        appName: {
          field: "APP_NAME",
          type: STRING(50),
          comment: "应用名称"
        },
        appCode: {
          field: "APP_CODE",
          type: STRING(50),
          comment: "应用编码"
        },
        appRemark: {
          field: "APP_REMARK",
          type: STRING(500),
          comment: "备注"
        },
        ...COMMON_FIELDS
      },
      virtuals: {
        ...COMMON_VIRTUAL_FIELDS
      }
    },
    device: {
      table: "ST_DEVICE",
      columns: {
        deviceId: {
          field: "DEVICE_ID",
          type: STRING,
          primaryKey: true,
          comment: "设备ID"
        },
        appId: {
          field: "APP_ID",
          type: STRING,
          comment: "应用ID"
        },
        deviceName: {
          field: "DEVICE_NAME",
          type: STRING(50),
          comment: "设备名称"
        },
        deviceProtocol: {
          field: "DEVICE_PROTOCOL",
          type: STRING(20),
          comment: "设备访问协议"
        },
        deviceIp: {
          field: "DEVICE_IP",
          type: STRING(200),
          comment: "设备访问域名"
        },
        devicePort: {
          field: "DEVICE_PORT",
          type: STRING(20),
          comment: "设备访问端口"
        },
        deviceAccount: {
          field: "DEVICE_ACCOUNT",
          type: STRING(200),
          comment: "设备账号"
        },
        devicePassword: {
          field: "DEVICE_PASSWORD",
          type: STRING(200),
          comment: "设备密码"
        },
        originLiveUrl: {
          field: "ORIGIN_LIVE_URL",
          type: STRING(200),
          comment: "设备原始直播流地址"
        },
        rtspUrl: {
          field: "RTSP_URL",
          type: STRING(200),
          comment: "通过流媒体服务平台转流后的rtsp地址"
        },
        rtmpUrl: {
          field: "RTMP_URL",
          type: STRING(200),
          comment: "通过流媒体服务平台转流后的rtmp地址"
        },
        hlsUrl: {
          field: "HLS_URL",
          type: STRING(200),
          comment: "通过流媒体服务平台转流后的hls地址"
        },
        wstsUrl: {
          field: "WSTS_URL",
          type: STRING(200),
          comment: "通过流媒体服务平台转流后的WS-TS地址"
        },
        mp4Url: {
          field: "MP4_URL",
          type: STRING(200),
          comment: "通过流媒体服务平台转流后的MP4-TS地址"
        },
        hls: {
          field: "HLS",
          type: STRING(1),
          comment: "是否支持转hls"
        },
        mp4: {
          field: "MP4",
          type: STRING(1),
          comment: "是否开启MP4录制"
        },
        timeout: {
          field: "TIMEOUT",
          type: INTEGER,
          comment: "拉流超时时间，单位秒"
        },
        rtpType: {
          field: "RTP_TYPE",
          type: STRING(200),
          comment: "拉流方式"
        },
        ...COMMON_FIELDS
      },
      virtuals: {
        hlsAble: {
          type: VIRTUAL,
          get() {
            return this.getDataValue("hls") === "0" ? "否" : "是";
          }
        },
        mp4Able: {
          type: VIRTUAL,
          get() {
            return this.getDataValue("mp4") === "0" ? "否" : "是";
          }
        },
        ...COMMON_VIRTUAL_FIELDS
      }
    }
  },
  dialect: {
    mysql: {
      development: config.development,
      test: config.test,
      production: config.production
    }
  }
};
